/*
 * @Description:
 * @Autor: liujinyin
 * @Date: 2021-06-28 18:40:43
 * @LastEditors: liujinyin
 * @LastEditTime: 2021-07-02 19:00:04
 */
package cipher

import (
	"gitee.com/micro-plat/sas/sas/modules/const/enum"
	"gitee.com/micro-plat/sas/sas/modules/const/models"
	"gitee.com/micro-plat/sas/sas/modules/secret"
	"github.com/micro-plat/hydra"
)

//SetHandler .
type SetHandler struct {
	sLib secret.ISecret
}

//NewSetHandler .
func NewSetHandler() (u *SetHandler) {
	return &SetHandler{
		sLib: secret.NewSecret(),
	}
}

// Handle 设置密钥
func (o *SetHandler) Handle(ctx hydra.IContext) (r interface{}) {
	ctx.Log().Debug("------设置密钥------")
	ctx.Log().Debug("1.检查参数")
	if err := ctx.Request().Check("euid"); err != nil {
		return err
	}

	if err := enum.CheckType(ctx.Request().Path().Params().GetString("type")); err != nil {
		return err
	}

	item := &models.Item{}
	if err := ctx.Request().Bind(item); err != nil {
		return err
	}

	ctx.Log().Debug("2.设置密钥")
	res, err := o.sLib.Set(
		ctx.Request().GetString("euid"),
		ctx.Request().Path().Params().GetString("type"),
		item)
	if err != nil {
		return err
	}

	return res
}
